package com.yeyks.cnbooking.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yeyks.cnbooking.Result.hotellistsearch.CbHotelList;
import com.yeyks.cnbooking.Result.hotellistsearch.CbHotelListSearchData;
import com.yeyks.cnbooking.Result.hotelsearch.CbHotel;
import com.yeyks.cnbooking.Result.hotelsearch.CbHotelSearchData;
import com.yeyks.cnbooking.Result.hotelsearch.CbReserve1;
import com.yeyks.cnbooking.Result.rateplansearch.*;
import com.yeyks.cnbooking.Result.roomsearch.CbRHotel;
import com.yeyks.cnbooking.Result.roomsearch.CbRoom;
import com.yeyks.cnbooking.Result.roomsearch.CbRoomSearchData;
import com.yeyks.cnbooking.base.CNResponse;
import com.yeyks.cnbooking.base.CbHotels;
import com.yeyks.cnbooking.base.CbImage;
import com.yeyks.cnbooking.base.CbImages;
import com.yeyks.cnbooking.dto.calendar.RoomCalendarDTO;
import com.yeyks.cnbooking.dto.hotel.HotelInfoDTO;
import com.yeyks.cnbooking.dto.room.RoomInfoDTO;
import com.yeyks.cnbooking.dto.room.RoomPriceNowDTO;
import com.yeyks.cnbooking.param.hotel.CbHotelListSearchParam;
import com.yeyks.cnbooking.param.hotel.CbHotelListSearchStayDateRange;
import com.yeyks.cnbooking.param.hotel.CbHotelSearchParam;
import com.yeyks.cnbooking.param.rateplansearch.CbRatePlanSearchParam;
import com.yeyks.cnbooking.param.roomsearch.CbRoomSearchParam;
import com.yeyks.cnbooking.service.CNBookingService;
import com.yeyks.cnbooking.service.TransitionService;
import com.yeyks.common.appcommon.json.JsonImageRoom;
import com.yeyks.common.base.RoomInfoDevicesJson;
import com.yeyks.common.base.RoomInfoRoomInfoJson;
import com.yeyks.common.constants.cnbooking.EsCnPrice;
import com.yeyks.common.constants.hotel.RoomInfoIconUrlConstant;
import com.yeyks.common.constants.hotel.RoomInfoRoomInfoJsonTypeConstant;
import com.yeyks.common.em.cnbooking.CnStarLineTypeEnum;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.common.utils.JsonUtils;
import com.yeyks.common.utils.apputil.enums.DevicesEnum;
import com.yeyks.common.utils.apputil.redis.RedisService;
import com.yeyks.common.utils.apputil.rediskey.RedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author shuai
 * @date 2019/09/26 16:38
 */
@Service
@Slf4j
public class TransitionServiceImpl implements TransitionService {

    @Autowired
    private CNBookingService cnBookingService;

    /**
     * 设置默认的 价格计划ID （校验时赋值）
     */
    private String ratePlanId = "";

    @Autowired
    private RedisService redisService;

    /**
     * 酒店查询
     *
     * @param countryId 国家id
     * @param hotelId   酒店id
     * @return
     */
    @Override
    public HotelInfoDTO hotelSearch(String countryId, String hotelId) {
        try {
            CbHotelSearchParam hotelSearchParam = new CbHotelSearchParam();
            hotelSearchParam.setCountryId(countryId);
            hotelSearchParam.setHotelId(hotelId);
            CNResponse<CbHotelSearchData> cnResponse = cnBookingService.hotelSearch(hotelSearchParam);
            CbHotelSearchData data = cnResponse.getData();
            CbHotels<CbHotel> hotels = data.getHotels();
            List<CbHotel> hotel = hotels.getHotel();
            return getHotelInfoDTO(hotel.get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HotelInfoDTO();
    }

    /**
     * 酒店对象转换
     *
     * @param cbHotel
     * @return
     */
    private HotelInfoDTO getHotelInfoDTO(CbHotel cbHotel) {
        Date date = new Date();
        HotelInfoDTO hotelInfoDTO = new HotelInfoDTO();
        if (null != cbHotel.getReserve3()
                && null != cbHotel.getReserve3().getServices()
                && null != cbHotel.getReserve3().getServices().getService()) {
            hotelInfoDTO.setServices(cbHotel.getReserve3().getServices().getService());
        }
        hotelInfoDTO.setHotelId(cbHotel.getHotelId());
        hotelInfoDTO.setGmtCreate(date);
        hotelInfoDTO.setGmtModified(date);
        hotelInfoDTO.setName(cbHotel.getHotelName());
        hotelInfoDTO.setDescription(cbHotel.getIntro());
        hotelInfoDTO.setAddress(cbHotel.getAddress());
        hotelInfoDTO.setStarLevel(CnStarLineTypeEnum.CnToMyStar.getDesc(cbHotel.getStar()));
        hotelInfoDTO.setComfortLevel(CnStarLineTypeEnum.CnToMyComfort.getDesc(cbHotel.getStar()));
        hotelInfoDTO.setVipFlag(0);
        hotelInfoDTO.setEnabledCoupon(1);
        hotelInfoDTO.setStatus(3);
        hotelInfoDTO.setPositionX(cbHotel.getLon());
        hotelInfoDTO.setPositionY(cbHotel.getLat());
        hotelInfoDTO.setValid(true);
        hotelInfoDTO.setIsDel(1);
        hotelInfoDTO.setManualPickOrder(0);
        hotelInfoDTO.setTel(cbHotel.getReserve2());
        hotelInfoDTO.setPhone(cbHotel.getReserve2());
        hotelInfoDTO.setEmail(cbHotel.getEmail());
        List<CbReserve1> reserve1 = cbHotel.getReserve1();
        if (!CollectionUtils.isEmpty(reserve1)) {
            CbReserve1 cbReserve1 = reserve1.get(0);
            if (null != cbReserve1 && cbReserve1.getImages() != null) {
                hotelInfoDTO.setExtraInfo(JsonUtils.addJsonList("imgUrlList", getImageUrls(cbReserve1.getImages())));
            }
        }
        return hotelInfoDTO;
    }

    /**
     * 酒店列表查询
     *
     * @param param
     * @return
     */
    @Override
    public List<HotelInfoDTO> hotelListSearch(CbHotelListSearchParam param) {
        try {
            CNResponse<CbHotelListSearchData> cnResponse = cnBookingService.hotelListSearch(param);
            if (null == cnResponse) {
                return Lists.newArrayList();
            }
            CbHotelListSearchData cnResponseData = cnResponse.getData();
            if (null == cnResponseData) {
                return Lists.newArrayList();
            }
            CbHotels<CbHotelList> hotels = cnResponseData.getHotels();
            return getHotelInfoDTOToList(hotels.getHotel());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Lists.newArrayList();
    }


    /**
     * 酒店对象转换
     *
     * @param cbHotelLists
     * @return
     */
    private List<HotelInfoDTO> getHotelInfoDTOToList(List<CbHotelList> cbHotelLists) {
        List<HotelInfoDTO> hotelInfoDTOS = Lists.newArrayList();
        if (null == cbHotelLists || 0 == cbHotelLists.size()) {
            return hotelInfoDTOS;
        }
        for (CbHotelList cbHotelList : cbHotelLists) {
            try {
                CbHotel cbHotel = BeanUtil.toBean(cbHotelList, CbHotel.class);
                HotelInfoDTO hotelInfoDTO = getHotelInfoDTO(cbHotel);
                if (null != cbHotelList.getImages()) {
                    hotelInfoDTO.setExtraInfo(JsonUtils.addJsonList("imgUrlList", getImageUrls(cbHotelList.getImages())));
                }
                if (null != cbHotelList.getTelephone()) {
                    hotelInfoDTO.setPhone(cbHotelList.getTelephone());
                }
                if (null == hotelInfoDTO.getServices() || 0 == hotelInfoDTO.getServices().size()) {
                    hotelInfoDTO.setServices(null == cbHotelList.getServices() ? null : cbHotelList.getServices().getService());
                }
                hotelInfoDTOS.add(hotelInfoDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return hotelInfoDTOS;
    }

    private List<String> getImageUrls(CbImages cbImages) {
        List<CbImage> image = cbImages.getImage();
        List<String> images = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(image)) {
            for (CbImage cbImage : image) {
                images.add(cbImage.getImageUrl());
            }
        }
        return images;
    }


    /**
     * 房间对象查询转换
     *
     * @param countryId 国家ID
     * @param hotelId   酒店Id
     * @return 返回结果
     */
    @Override
    public List<RoomInfoDTO> roomSearch(String countryId, String provinceId,
                                        String cityId, String hotelId,
                                        Double lowPercent, Double highPercent) {

        CbRoomSearchParam roomSearchParam = new CbRoomSearchParam();
        roomSearchParam.setCountryId(countryId);
        roomSearchParam.setProvinceId(provinceId);
        roomSearchParam.setCityId(cityId);
        roomSearchParam.setHotelId(hotelId);
        CNResponse<CbRoomSearchData> cbRoomSearchDataCNResponse = cnBookingService.roomSearch(roomSearchParam);
        try {
            return convertRoomInfo(cbRoomSearchDataCNResponse.getData(), countryId, hotelId, lowPercent, highPercent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Map <String, List<RoomCalendarDTO>>
     *
     * @param countryId
     * @param provinceId
     * @param cityId
     * @param hotelId
     * @param roomId
     * @param checkIn
     * @param checkOut
     * @return
     */
    @Override
    public Map<String, List<RoomCalendarDTO>> mapRoomCalendarSearch(String countryId,
                                                                    String provinceId,
                                                                    String cityId,
                                                                    String hotelId,
                                                                    String roomId,
                                                                    String checkIn,
                                                                    String checkOut,
                                                                    Double lowPercent,
                                                                    Double highPercent) {
        CbRatePlanSearchParam planSearchParam = new CbRatePlanSearchParam();
        planSearchParam.setCountryId(countryId);
        planSearchParam.setHotelId(hotelId);
        planSearchParam.setProvinceId(provinceId);
        planSearchParam.setCityId(cityId);
        if(!StringUtils.isEmpty(roomId)){
            planSearchParam.setRoomId(roomId);
        }
        CbHotelListSearchStayDateRange range = new CbHotelListSearchStayDateRange();
        range.setCheckIn(checkIn);
        range.setCheckOut(checkOut);
        planSearchParam.setStayDateRange(range);
        //查询房态信息
        CNResponse<CbRatePlanSearchData> cbRatePlanSearchDataCNResponse = cnBookingService.ratePlanSearch(planSearchParam);

        try {
            //新建一个Map集合，放入房间对应房态的信息；
            Map<String, List<RoomCalendarDTO>> roomCalendarMap = Maps.newHashMap();
            if(null == cbRatePlanSearchDataCNResponse.getData()){
                return null;
            }
            if(null == cbRatePlanSearchDataCNResponse.getData().getHotels()){
                return null;
            }
            List<CbRatePlanHotel> cbRatePlanHotelList = cbRatePlanSearchDataCNResponse.getData().getHotels().getHotel();
            if (CollectionUtils.isEmpty(cbRatePlanHotelList)) {
                return null;
            }
            CbRatePlanHotel ratePlanHotel = cbRatePlanHotelList.get(0);
            if (null == ratePlanHotel || ratePlanHotel.getRooms() == null) {
                return null;
            }
            List<CbRatePlanRoom> cbRatePlanRoomList = ratePlanHotel.getRooms().getRoom();
            if (CollectionUtils.isEmpty(cbRatePlanRoomList)) {
                return null;
            }
            for(CbRatePlanRoom cbRatePlanRoom : cbRatePlanRoomList){
                //初始化
                ratePlanId = "";
                //验证当前房间的房态信息--》 并且如果通过，给 ratePlanId进行赋值;
                if (!validRatePlan(cbRatePlanRoom)) {
                    //如果房态验证不通过，跳过这个房间
                    continue;
                }
                //得到当前房间的房态信息
                List<RoomCalendarDTO> roomCalendarDTOS = convertRoomCalendar(cbRatePlanRoom, lowPercent, highPercent);
                if(!CollectionUtils.isEmpty(roomCalendarDTOS)){
                    //将信息存入Map中
                    roomCalendarMap.put(cbRatePlanRoom.getRoomId(), roomCalendarDTOS);
                }
            }
            //返回最终信息
            return roomCalendarMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转换房间对象
     *
     * @param roomSearchData 酒店房间源数据
     * @return 返回房间列表
     */
    private List<RoomInfoDTO> convertRoomInfo(CbRoomSearchData roomSearchData, String countryId, String hotelId, Double lowPercent,
                                              Double highPercent) {

        List<RoomInfoDTO> roomList = Lists.newArrayList();

        if (null != roomSearchData && roomSearchData.getHotels() != null) {
            //初始化count
            CbHotels<CbRHotel> hotels = roomSearchData.getHotels();
            RoomInfoDTO roomInfoDTO = null;
            if (Integer.parseInt(hotels.getHotelCount()) > 0) {
                CbRHotel cbRHotel = hotels.getHotel().get(0);
                if (Integer.parseInt(cbRHotel.getRooms().getRoomCount()) > 0) {
                    List<CbRoom> cbRoomList = cbRHotel.getRooms().getRoom();
                    Map<String, RoomPriceNowDTO> map = this.getNowHotelByRoomPrice( countryId,
                            hotelId, lowPercent, highPercent);
                    for (CbRoom room : cbRoomList) {
                        roomInfoDTO = convertRoomDTO(room, map);
                        if ( null == roomInfoDTO ){
                            continue;
                        }
                        roomList.add(roomInfoDTO);
                    }
                }
            }
        }
        return roomList;
    }

    /**
     * 组装房间对象信息
     * @param room 房间对象
     * @param map 房间价格Map
     * @return 返回结果
     */
    private RoomInfoDTO convertRoomDTO(CbRoom room,
                                       Map<String, RoomPriceNowDTO> map){
        RoomInfoDTO roomInfoDTO = new RoomInfoDTO();
        roomInfoDTO.setCount(Integer.valueOf(room.getRoomAcount()));

        //价格要在价格计划中查询
        //(1) 得到当天的价格计划
        RoomPriceNowDTO dto = map.get(room.getRoomId());
        if(null != dto){
            roomInfoDTO.setRatePlanId(dto.getRatePlanId());
            roomInfoDTO.setCurPrice(dto.getCurPrice());
            roomInfoDTO.setBottomPrice(dto.getBottomPrice());
            roomInfoDTO.setOrigPrice(dto.getOrigPrice());
            roomInfoDTO.setDefaultCurPrice(dto.getCurPrice());
        }else{
            return null;
        }
        //面积大小
        roomInfoDTO.setArea(null);
        //最多可住几人
        roomInfoDTO.setMaxPeople(Integer.valueOf(room.getMaxAdult()));
        //图片
        if (null != room.getImages() && !CollectionUtils.isEmpty(room.getImages().getImage())) {
            List<String> picUrls = room.getImages().getImage().stream().map(CbImage::getImageUrl).collect(Collectors.toList());
            roomInfoDTO.setPicUrls(picUrls);
            JsonImageRoom roomImages = new JsonImageRoom();
            roomImages.setImgUrlList(picUrls);
            roomInfoDTO.setExtraInfo(JSON.toJSONString(roomImages));
        }
        //默认服务
        String devices = DevicesEnum.Internet.getEn();
        roomInfoDTO.setDevices(devices);
        roomInfoDTO.setTitle(room.getRoomName());
        roomInfoDTO.setBedType(room.getBedType());
        roomInfoDTO.setId(Integer.valueOf(room.getRoomId()));
        roomInfoDTO.setValid(true);
        //客房信息
        List<RoomInfoRoomInfoJson> roomInfoRoomInfoJsons = this.roomInfoRoomInfoJsonListConvert(room);
        roomInfoDTO.setRoomInfoJson(JSON.toJSONString(roomInfoRoomInfoJsons));
        //设备信息
        List<RoomInfoDevicesJson> roomInfoDevicesJsons = this.roomInfoDevicesJsonListConvert(room);
        roomInfoDTO.setDevicesJson(JSON.toJSONString(roomInfoDevicesJsons));
        return roomInfoDTO;
    }

    /**
     * 客房的相关信息
     *
     * @param room CNBooking 的房间对象
     */
    private List<RoomInfoRoomInfoJson> roomInfoRoomInfoJsonListConvert(CbRoom room) {

        List<RoomInfoRoomInfoJson> list = Lists.newArrayList();
        //面积
        if(!StringUtils.isEmpty(room.getAcreage())){
            String acreage = room.getAcreage() + "m²";
            if("0".equals(room.getAcreage())){
                acreage = "到店查看";
            }
            RoomInfoRoomInfoJson areas = new RoomInfoRoomInfoJson(acreage,
                    RoomInfoIconUrlConstant.AREA,
                    RoomInfoRoomInfoJsonTypeConstant.AREA,
                    room.getAcreage());
            list.add(areas);
        }
        //人数
        if(!StringUtils.isEmpty(room.getMaxAdult())){
            String maxAdult = "最多可住" + room.getMaxAdult() + "人";
            if("0".equals(room.getMaxAdult())){
                maxAdult = "到店查看";
            }
            RoomInfoRoomInfoJson maxPerson = new RoomInfoRoomInfoJson(maxAdult,
                    RoomInfoIconUrlConstant.PEOPLE_NUMBER,
                    RoomInfoRoomInfoJsonTypeConstant.PEOPLE_NUMBER,
                    room.getMaxAdult());
            list.add(maxPerson);
        }
        //楼层
        if(!StringUtils.isEmpty(room.getFloor())){
            String floors = room.getFloor() + "层";
            if("0".equals(room.getFloor())){
                floors = "到店查看";
            }
            RoomInfoRoomInfoJson floor = new RoomInfoRoomInfoJson(floors,
                    RoomInfoIconUrlConstant.FLOOR,
                    RoomInfoRoomInfoJsonTypeConstant.FLOOR,
                    room.getFloor());
            list.add(floor);
        }
        //床型
        if(!StringUtils.isEmpty(room.getBedType())){
            RoomInfoRoomInfoJson bed = new RoomInfoRoomInfoJson(room.getBedType(),
                    RoomInfoIconUrlConstant.BED_TYPE,
                    RoomInfoRoomInfoJsonTypeConstant.BED_TYPE,
                    room.getBedType());
            list.add(bed);
        }
        return list;
    }

    /**
     * 客房设备信息
     *
     * @param room CNBooking 的房间对象
     * @return
     */
    private List<RoomInfoDevicesJson> roomInfoDevicesJsonListConvert(CbRoom room) {

        List<RoomInfoDevicesJson> roomInfoDevicesJsons = Lists.newArrayList();
        RoomInfoDevicesJson wifi = new RoomInfoDevicesJson();
        if ("0".equals(room.getHasNet())) {
            wifi.setName("无");
        } else {
            wifi.setName("宽带WIFI");
            wifi.setIcon(RoomInfoIconUrlConstant.RIGHT);
            roomInfoDevicesJsons.add(wifi);
        }
        RoomInfoDevicesJson isWindow = new RoomInfoDevicesJson();
        if ("0".equals(room.getHasWindow())) {
            isWindow.setName("无窗");
        } else if ("1".equals(room.getHasWindow())) {
            isWindow.setName("窗户");
            isWindow.setIcon(RoomInfoIconUrlConstant.RIGHT);
            roomInfoDevicesJsons.add(isWindow);
        }
        //吸烟
        RoomInfoDevicesJson smoke = new RoomInfoDevicesJson();
        if ("0".equals(room.getAllowSmoke())) {
            smoke.setName("不可吸烟");
        } else if ("1".equals(room.getAllowSmoke())) {
            smoke.setName("可吸烟");
            smoke.setIcon(RoomInfoIconUrlConstant.RIGHT);
            roomInfoDevicesJsons.add(smoke);
        } else if ("2".equals(room.getAllowSmoke())) {
            smoke.setName("部分吸烟");
        }
        return roomInfoDevicesJsons;
    }

    /**
     * 房态的转换
     *
     * @param cbRatePlanRoom xml的对象参数
     * @return 返回转换结果
     */
    private List<RoomCalendarDTO> convertRoomCalendar(CbRatePlanRoom cbRatePlanRoom, Double lowPercent, Double highPercent) {


        List<RoomCalendarDTO> roomCalendarDTOList = Lists.newArrayList();
        RoomCalendarDTO roomCalendarDTO = null;
        if (null != cbRatePlanRoom) {

            if (Integer.valueOf(cbRatePlanRoom.getRates().getRateCount()) > 0) {

                List<CbRate> cbRateList = cbRatePlanRoom.getRates().getRate();
                if (!CollectionUtils.isEmpty(cbRateList)) {
                    for (CbRate cbRate : cbRateList) {
                        //录入选择的房态计划
                        if (!cbRate.getRatePlanId().equals(ratePlanId)) {
                            continue;
                        }
                        List<CbPriceAndStatu> priceAndStatuList = cbRate.getPriceAndStatus().getPriceAndStatu();
                        if (!CollectionUtils.isEmpty(priceAndStatuList)) {
                            for (CbPriceAndStatu priceAndStatu : priceAndStatuList) {
                                roomCalendarDTO = new RoomCalendarDTO();

                                roomCalendarDTO.setDate(DateUtils.strXmlToDate(priceAndStatu.getDate()));

                                if(StringUtils.isEmpty(priceAndStatu.getPrice())){
                                   continue;
                                }

                                //现价上浮10个点 （价格取整）
                                int curPrice = (int) (Double.parseDouble(priceAndStatu.getPrice()) * highPercent) * 100;
                                //底价5个点 （价格取整）
                                int bottomPrice = (int) (Double.parseDouble(priceAndStatu.getPrice()) * lowPercent) * 100;


                                roomCalendarDTO.setCount(Integer.valueOf(priceAndStatu.getCount()));

                                // * 026001 开启
                                // * 026002 冻结
                                int status = 1;
                                if ("026002".equals(priceAndStatu.getStatu())) {
                                    //状态为不启用的数据
                                    int originPrice = Double.valueOf(priceAndStatu.getPrice()).intValue();
                                    String calendarId = "cnbooking_calendar_" + priceAndStatu.getRateId() +
                                            DateUtils.toYYMMddStr(DateUtils.strXmlToDate(priceAndStatu.getDate()));
                                    if(originPrice >= 99999){
                                        redisService.setAndExpire(RedisKey.CNBOOKING_MAX_PRICE + ":" + calendarId,
                                                JSON.toJSONString(priceAndStatu),
                                                10,
                                                TimeUnit.DAYS);
                                    }else{
                                        roomCalendarDTO.setBottomPrice(bottomPrice);
                                        roomCalendarDTO.setSalePrice(curPrice);
                                        roomCalendarDTO.setOriginPrice((int) (Double.parseDouble(priceAndStatu.getPrice()) * 100));
                                    }
                                    status = 0;
                                }else{
                                    roomCalendarDTO.setBottomPrice(bottomPrice);
                                    roomCalendarDTO.setSalePrice(curPrice);
                                    roomCalendarDTO.setOriginPrice((int) (Double.parseDouble(priceAndStatu.getPrice()) * 100));
                                }
                                roomCalendarDTO.setStatus(status);
                                roomCalendarDTO.setId(Long.valueOf(priceAndStatu.getRateId()));
                                roomCalendarDTO.setRoomId(Integer.valueOf(cbRatePlanRoom.getRoomId()));
                                //价格计划ID
                                roomCalendarDTO.setRatePlanId(cbRate.getRatePlanId());
                                roomCalendarDTOList.add(roomCalendarDTO);
                            }
                        }
                    }
                }
            }
        }
        return roomCalendarDTOList;
    }


    /**
     * 验证价格计划是否录入（提前预订天数,例如：0
     * CutOffDay;
     * 提前预订天数的具体时间,例如：23:59
     * CutOffTime;
     *
     * @param cbRatePlanRoom 价格计划【房间】对象
     * @return true，可以录入， false，跳过不进行存储
     */
    private Boolean validRatePlan(CbRatePlanRoom cbRatePlanRoom) {

        boolean flag = false;
        //初始化ratePlanId
        if (null != cbRatePlanRoom) {
            if (Integer.parseInt(cbRatePlanRoom.getRatePlans().getRatePlanCount()) > 0) {
                List<CbRatePlan> cbRatePlanList = cbRatePlanRoom.getRatePlans().getRatePlan();
                if (!CollectionUtils.isEmpty(cbRatePlanList)) {
                    for (CbRatePlan cbRatePlan : cbRatePlanList) {
                        ratePlanId = "";
                        //验证参数是否跳过
                        if ("0".equals(cbRatePlan.getCutOffDay()) && "23:59".equals(cbRatePlan.getCutOffTime())) {
                            //给价格计划ID 赋值
                            ratePlanId = cbRatePlan.getRatePlanId();

                            //如果价格计划的日期都为0，那么就不录入房间
                            int rateCount = Integer.parseInt(cbRatePlanRoom.getRates().getRateCount());
                            if (rateCount == 0) {
                                flag = false;
                            }else {
                                //判断PriceAndStatuCount是否为0
                                List<CbRate> rate = cbRatePlanRoom.getRates().getRate();
                                for(CbRate cbRate : rate){
                                    if(ratePlanId.equals(cbRate.getRatePlanId())){
                                        if(!StringUtils.isEmpty(cbRate.getPriceAndStatus().getPriceAndStatuCount())){
                                            int priceAndStatuCount = Integer.parseInt(cbRate.getPriceAndStatus().getPriceAndStatuCount());
                                            if(priceAndStatuCount > 0){
                                                flag = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                //如果是true，跳出循环
                                if(flag){
                                    break;
                                }
                            }
                        } /*else {
                            //该房间的 房态数据不满足条件， 已过滤，数据如 cbRatePlan.getCutOffDay(), cbRatePlan.getCutOffTime()
                        }*/
                    }
                }
            }
        }
        return flag;
    }

    private Map<String, RoomPriceNowDTO> getNowHotelByRoomPrice(String countryId, String hotelId, Double lowPercent,
                                                                Double highPercent){
        Map<String, RoomPriceNowDTO> map = Maps.newHashMap();
        CbRatePlanSearchParam param = new CbRatePlanSearchParam();
        param.setCountryId(countryId);
        param.setHotelId(hotelId);
        CbHotelListSearchStayDateRange range = new CbHotelListSearchStayDateRange();
        range.setCheckIn(DateUtils.toYYMMddStr(new Date()));
        range.setCheckOut(DateUtils.toYYMMddStr(DateUtils.dayAfter(new Date(), 1)));
        param.setStayDateRange(range);
        CNResponse<CbRatePlanSearchData> cnResponse = cnBookingService.ratePlanSearch(param);

        String price = "0.00";

        if(null == cnResponse.getData()){
            return map;
        }
        if(null == cnResponse.getData().getHotels()){
            return map;
        }
        List<CbRatePlanHotel> cbHotelList = cnResponse.getData().getHotels().getHotel();
        if (CollectionUtils.isEmpty(cbHotelList)) {
            log.warn( "没有酒店" );
            return map;
        }
        if (cbHotelList.get(0).getRooms() == null) {
            log.warn( "该酒店没有房间" );
            return map;
        }
        List<CbRatePlanRoom> cbRatePlanRoomList = cbHotelList.get(0).getRooms().getRoom();
        if (CollectionUtils.isEmpty(cbRatePlanRoomList)) {
            log.warn( "没有房间" );
            return map;
        }
        for (CbRatePlanRoom cbRatePlanRoom : cbRatePlanRoomList) {
            RoomPriceNowDTO dto = new RoomPriceNowDTO( );
            //每一次进来进行初始化
            ratePlanId = "";
            if ( !validRatePlan( cbRatePlanRoom ) ) {
                continue;
            }
            //验证通过后给【价格计划ID】存值
            dto.setRatePlanId( ratePlanId );
            if ( cbRatePlanRoom.getRates( ) == null ) {
                continue;
            }

            List<CbRate> cbRateList = cbRatePlanRoom.getRates( ).getRate( );
            //价格计划的验证
            if ( CollectionUtils.isEmpty( cbRateList ) ) {
                continue;
            }
            for (CbRate cbRate : cbRateList) {
                List<CbPriceAndStatu> priceAndStatuList = cbRate.getPriceAndStatus( ).getPriceAndStatu( );
                if ( CollectionUtils.isEmpty( priceAndStatuList ) ) {
                    continue;
                }
                price = priceAndStatuList.get( 0 ).getPrice( );
                break;
            }
            //现价上浮5个点 （价格取整）
            int curPrice = (int) (Double.parseDouble( price ) * highPercent) * 100;
            //底价10个点 （价格取整）
            int bottomPrice = (int) (Double.parseDouble( price ) * lowPercent) * 100;
            dto.setCurPrice( curPrice );
            dto.setBottomPrice( bottomPrice );
            dto.setOrigPrice( (int) (Double.parseDouble( price ) * 100) );
            //存入信息
            map.put( cbRatePlanRoom.getRoomId(), dto );
        }
        return map;
    }
}
